package com.wanhai.itm.controller;

import com.wanhai.itm.entity.User;
import com.wanhai.itm.services.AuthenticationService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;

/**
 *
 * @author Richard Shih
 */
public class LoginController extends SelectorComposer<Component> {

    @Wire
    private Textbox userid;
    @Wire
    private Textbox password;
    @Wire
    private Label message;
    //service
    AuthenticationService authService = AuthenticationService.getDefaultService();

    @Listen("onClick=#login; onOK=#loginWin")
    public void doLogin() {
        String id = userid.getValue();
        String pw = password.getValue();

        if (!authService.login(id, pw)) {
            message.setValue("User Id or Password not match!");
            return;
        }

        User user = authService.getAuthorizedUser();
        message.setValue("Welcome " + user.getUserName());

        Executions.sendRedirect("/index.zul");
    }
}
